package com.daguchuangyi.erp_android.productlist.select;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.ErpApplication;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.manager.ParameterManager;
import com.daguchuangyi.erp_android.model.product.Parameter;
import com.daguchuangyi.erp_android.model.product.Product;
import com.daguchuangyi.erp_android.production_detail.ProductDetailActivity;
import com.daguchuangyi.erp_android.productlist.ProductSummaryManager;
import com.daguchuangyi.erp_android.util.ImageUtil;
import com.guanaj.easyswipemenulibrary.EasySwipeMenuLayout;

import java.util.List;
import java.util.Set;

/**
 * Created by lijianjian on 2018/3/11.
 */

public class GroupProductAdapter extends BaseQuickAdapter<GroupProductModel, BaseViewHolder> {

    public GroupProductAdapter(int layoutResId, @Nullable List<GroupProductModel> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, GroupProductModel item) {
        Context context = helper.itemView.getContext();
//        helper.setText(R.id.group_title_tv, "刺绣／条边／水溶条边");
        helper.setText(R.id.group_title_tv, item.getCategroryName());
        RecyclerView recyclerView = helper.getView(R.id.sub_item_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        List<Product> models = item.getSubModels();
        SubItemAdapter subItemAdapter = new SubItemAdapter(R.layout.item_sub_product_layout, models);
        subItemAdapter.mOnItemRemovedListener = new OnItemRemovedListener() {
            @Override
            public void onItemRemove() {
                if (item.getSubModels().size() == 0) {
                    getData().remove(item);
                    notifyItemRemoved(helper.getAdapterPosition());
                }
            }
        };
        recyclerView.setAdapter(subItemAdapter);
    }

    private class SubItemAdapter extends BaseQuickAdapter<Product, BaseViewHolder> {

        OnItemRemovedListener mOnItemRemovedListener;

        public SubItemAdapter(int layoutResId, @Nullable List<Product> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Product item) {
            Context context = helper.itemView.getContext();
            helper.getView(R.id.right_menu).setOnClickListener(v -> {
                EasySwipeMenuLayout easySwipeMenuLayout = helper.getView(R.id.es);
                getData().remove(item);
                notifyItemRemoved(helper.getAdapterPosition());
                easySwipeMenuLayout.resetStatus();
                ProductSummaryManager.getInstance().removeProductChecked(item);
                mOnItemRemovedListener.onItemRemove();
            });

            Glide.with(ErpApplication.getAppContext())
                    .load(item.getExhibitionImage())
                    .apply(ImageUtil.getProductSummaryOptions())
                    .into((ImageView) helper.getView(R.id.cover_one));


            helper.setText(R.id.title_tv, item.getPNO());
//            helper.setText(R.id.sub_title_tv, ParameterManager.getInstance().getParamsCheckContent());
            String descriptionStr = "";

            Set<Integer> localParams = ParameterManager.getInstance().getLocalParamsCheckedSet();
            if (localParams.contains(ParameterManager.ID_STOCK_OPTIONS)) {
                if (!TextUtils.isEmpty(descriptionStr)) {
                    descriptionStr += ("，仓储：" + item.getStorages().size());
                } else {
                    descriptionStr = ("仓储：" + item.getStorages().size());
                }
            }
            if (localParams.contains(ParameterManager.ID_SAMPLE_OPTIONS)) {
                if (!TextUtils.isEmpty(descriptionStr)) {
                    descriptionStr += ("，样品库：" + item.getShelves().size());
                } else {
                    descriptionStr = ("样品库：" + item.getShelves().size());
                }
            }
            for (int i = 0; i < item.getParameters().size(); i++) {
                Parameter productParam = item.getParameters().get(i);
                if (ParameterManager.getInstance().getParametersChecked().contains(productParam)) {
                    String value = productParam.getProductparameters().getValue();
                    if (TextUtils.isEmpty(value)) {
                        value = " ";
                    }
                    if (!TextUtils.isEmpty(descriptionStr)) {
                        descriptionStr += ("，" + productParam.getName() + "：" + value);
                    } else {
                        descriptionStr = productParam.getName() + "：" + value;
                    }
                }
            }
            if (localParams.contains(ParameterManager.ID_CAD_IMAGE_OPTIONS)) {
                helper.getView(R.id.cover_two).setVisibility(View.VISIBLE);
                Glide.with(ErpApplication.getAppContext())
                        .load(item.getCassetteImage())
                        .apply(ImageUtil.getProductSummaryOptions())
                        .into((ImageView) helper.getView(R.id.cover_two));
            } else {
                helper.getView(R.id.cover_two).setVisibility(View.GONE);
            }

//        ParameterManager.getInstance().getParametersChecked();
            helper.setText(R.id.sub_title_tv, descriptionStr);


            helper.getView(R.id.content).setOnClickListener(v -> {
                ProductDetailActivity.start(context, item.getId());
            });

        }
    }

    private interface OnItemRemovedListener {
        void onItemRemove();
    }

}
